const mysql = require('mysql')

module.exports = {
    config: {
        host: '42.193.174.155',
        port: '3306',
        user: 'xsz',
        password: 'zzzxxx123',
        database: 'xiaohei'
    },
    sqlConnection: function (sql, sqlArr, callBack) {
        let pool = mysql.createPool(this.config)
        pool.getConnection((err, conn) => {
            if (err) {
                console.log('连接失败');
                console.log(err)
                return
            }
            //事件驱动回调
            conn.query(sql, sqlArr, callBack);
            //释放连接
            conn.release()
        })
    },
    //promise回调 
    SySqlConnect: function (sySql, sqlArr) {
        return new Promise((resolve, reject) => {
            var pool = mysql.createPool(this.config);
            pool.getConnection(function (err, conn) {
                if (err) {
                    reject(err)
                } else {
                    conn.query(sySql, sqlArr, (err, data) => {
                        if (err) {
                            console.log(err, '错误')
                            reject(err)
                        } else {
                            resolve(data)
                        }
                        //释放连接
                        conn.release();
                    })
                }
            })
        }).catch((err) => {
            console.log(err)
        })
    }
}